#!/usr/bin/env jruby

# 
# This file is an example of using JRuby to select a directory using a GUI and then
# displays the size of each file in the command output.
#
# To run this:
#   jruby file_lister_with_gui_input.rb
#
# You can also run it on the command line exclusively by passing a directory to the script
# when running it (jruby file_lister_with_gui_input.rb .)
#
# You can find the sample code used for this file here:
# http://c2.com/cgi/wiki?JayRuby
#
# To download and learn about JRuby, go here:
# http://jruby.codehaus.org/
#
# Author: Elizabeth
#


# File file_system_lister.rb (2007-08-13)
# Use CLI or JFileChooser to select a directory for file system listing
# Don't bother loading the Java libraries if we don't need them


 require 'find'


 def list_file_sizes(dir_path)
   puts format('%10s : %s', 'BYTES', 'FILE PATH')
   Find.find(dir_path) do |f|
     puts format('%10d : %s', FileTest.size(f), f) if FileTest.file?(f)
   end
 end


 dir_path = ARGV[0]


 if dir_path.nil?
   require 'java'
   JFileChooser = javax.swing.JFileChooser


   chooser = JFileChooser.new  # starts at home directory by default
   chooser.set_file_selection_mode(JFileChooser::DIRECTORIES_ONLY)
   return_val = chooser.show_open_dialog(javax.swing.JPanel.new)


   if return_val == JFileChooser::APPROVE_OPTION
     dir_path = chooser.get_selected_file.get_path
   end
 end


 list_file_sizes(dir_path) unless dir_path.nil?
 exit  # important